<?php
/**
 * Project: AIGC
 * Author: moufer <moufer@163.com>
 * Date: 2024/9/23 23:28
 */

namespace module\user\enum;

use mof\annotation\Description;
use mof\concern\EnumExtend;
use mof\Module;

enum CaptchaEmsEventEnum: string
{
    use EnumExtend;

    #[Description('账号注册验证码')]
    case REGISTER = 'register';

    #[Description('重置密码验证码')]
    case FORGET = 'forget';

    #[Description('邮箱绑定验证码')]
    case BIND = 'bind';

    #[Description('邮箱解绑验证码')]
    case UNBIND = 'unbind';

    /**
     * 获取验证码模板文件地址
     * @return string
     */
    public function getMailTemplate(): string
    {
        $file = Module::path('user')
            . 'front' . DIRECTORY_SEPARATOR
            . 'stubs' . DIRECTORY_SEPARATOR
            . 'captcha_ems.html';

        if (!is_file($file)) {
            $content = '用户您好！<br><br>你的验证码是：{code}，请在{time}分钟内完成验证。';
        } else {
            $content = file_get_contents($file);
        }

        return $content;
    }

    /**
     * 是否需要登录
     * @return bool
     */
    public function needLogin(): bool
    {
        return in_array($this, [self::BIND, self::UNBIND]);
    }

    /**
     * 当前行为是否允许获取验证码
     * @return bool
     */
    public function isEnabled(): bool
    {
        $mobileFields = config('user.email_feature', []);
        $key = $this->value === self::UNBIND->value ? self::BIND->value : $this->value;
        return in_array($key, $mobileFields);
    }
}
